package com.breez.aiauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.breez.aiauth.common.config.handler.AiAuthException;
import com.breez.aiauth.common.constants.AuthConstant;
import com.breez.aiauth.common.util.UUIDUtil;
import com.breez.aiauth.core.aiface.AiAuthService;
import com.breez.aiauth.domain.FaceRequest;
import com.breez.aiauth.domain.SysUser;
import com.breez.aiauth.domain.dto.BindFaceDto;
import com.breez.aiauth.domain.dto.LoginDto;
import com.breez.aiauth.domain.dto.RegisterDto;
import com.breez.aiauth.service.AuthService;
import com.breez.aiauth.service.SysUserService;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private AiAuthService aiAuthService;
    @Autowired
    private SysUserService sysUserService;

    @Transactional
    @Override
    public JSONObject bindFace(BindFaceDto bindFaceDto) {
        HashMap<String, String> options = new HashMap<String, String>();
        String userId = bindFaceDto.getUserId();
        FaceRequest request = new FaceRequest();
        options.put("quality_control", "LOW");
        options.put("liveness_control", "NORMAL");
        options.put("action_type", "REPLACE");

        request.setImageType("BASE64");
        request.setUserId(userId);
        request.setGroupId("user");
        request.setOptions(options);
        request.setImage(bindFaceDto.getImage());

        String image = bindFaceDto.getImage();
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setBindFace(1);
        sysUser.setImage("data:image/jpeg;base64," + image);
        JSONObject jsonObject = null;
        try {
            jsonObject = aiAuthService.addUser(request);
            sysUserService.updateById(sysUser);
        } catch (Exception e) {
            aiAuthService.deleteUser(request);
        }
        return jsonObject;
    }

    @Override
    public JSONObject loginByFace(String face) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "3");
        options.put("match_threshold", "70");
        options.put("quality_control", "LOW");
        options.put("liveness_control", "NORMAL");
        options.put("max_user_num", "1");
        FaceRequest request = new FaceRequest();
        request.setImage(face);
        request.setImageType(AuthConstant.BASE64);
        request.setGroupIdList("user");
        request.setOptions(options);
        return aiAuthService.search(request);
    }

    @Override
    public boolean register(RegisterDto registerDto) throws AiAuthException {
        SysUser u = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, registerDto.getUsername()));
        if (!StringUtils.isEmpty(u)) {
            throw new AiAuthException(4002, "用户名已经存在");
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(registerDto, sysUser);
        String userId = UUIDUtil.userId();
        sysUser.setId(userId);
        return sysUserService.save(sysUser);
    }

    @Override
    public SysUser loginByAccount(LoginDto loginDto) {
     return sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, loginDto.getUsername())
                .eq(SysUser::getPassword, loginDto.getPassword()));

    }
}
